<?php
/**
 * Created by PhpStorm.
 * User: azhuang
 * Date: 2018/4/15
 * Time: 21:08
 */

namespace Admin\Controller;


use Think\Page;

class UserController extends BaseController
{
	public function add()
	{
//		判断是否是post提交过来的
		if (IS_POST) {
//			$post = I('post.');
			$model = D('User');
//			$result = $model->add($post);
			$data = $model->create();
//			echo $data;
			if (!$data) {
//				用户验证失败
//				echo $model->getError();
				$this->error($model->getError());
				die;
			}
//			dump($data);die;
			//添加时间字段
			$data['addtime'] = time();
			$result = $model->add($data);
			if ($result) {
				$this->success('添加成功', U('showList', 3));
			} else {
				$this->error('添加失败');
			}
		} else {
//获取所有的部门
			$modelUser = M('User');
			$modelDept = M('Dept');

//			$data = $model->where('pid = 0')->select();
			$resultDept = $modelDept->select();
//			dump($resultDept);die;
			$this->assign('resultDept', $resultDept);
//			$this->assign('data', $data);
			$this->display();
		}
	}

	public function showList()
	{
//		获取部门所有的信息
		$modelDept = M('Dept');
		$resultDept = $modelDept->select();
//			dump($resultDept);die;
		$model = D('User');
		$mDatas = $model->select();
//		分页第一步，查询记录数
		$mCount = $model->count();
//		实例化分页类
		$page = new Page($mCount, 1);
//		定义文字提示
		$page->rollPage = 5;
		$page->lastSuffix = false;
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$page->setConfig('last', '末页');
		$page->setConfig('first', '首页');
		//生成字符串html
		$show = $page->show();
		$this->assign('show', $show);
//查询每页对应的数据
		$mDatas = $model->limit($page->firstRow, $page->listRows)->select();

//		var_dump($result);die;
		//查询职员所对应的部门
		foreach ($mDatas as $key => $value) {
			//查询dept_id对应的部门
//			var_dump($value['dept_id']);
			if (!empty($value['dept_id'])) {
				$id = $value['dept_id'];
				$info = $modelDept->find($id);
				//只需要保留其中的name
				$mDatas[$key]['deptname'] = $info['name'];
			} else {
				$mDatas[$key]['deptname'] = '未设置该职员部门信息';
			}
		}
		$this->assign('mDatas', $mDatas);
//		var_dump($result);die;
		$this->display();
	}

	public function charts()
	{
//查询一个部门下的所有人数
		$model = M();
		$data = $model->field('t2.name as deptname ,count(*) as count')
			->table('sp_user as t1,sp_dept as t2')->where('t1.dept_id = t2.id')
			->group('deptname')->select();
//		dump($data);
//		die;
		$str = '[';
		foreach ($data as $key => $value) {
			$str .= "['" . $value['deptname'] . "'," . $value['count'] . "],";
		}
//		去除最后的字符串
		$str = rtrim($str, ',') . ']';
		$this->assign('str', $str);//string(49) "[['技术部',1],['设计部',1],['运营部',2]]"
		dump($str);//  				 string(49) "[['技术部',1],['设计部',1],['运营部',2]]"
		$this->display();
	}

	public function edit()
	{
		if (IS_POST) {
//修改确定后的操作
			$post = I('post.');
			$model = D('User');
//			$result = $model->add($post);
			$saveResult = $model->create();
			$post['addtime'] = time();
			if (!$saveResult) {
//				用户验证失败,用户是否提交完整的信息
//				echo $model->getError();
				$this->error($model->getError());
				die;
			}
//			dump($post) ;die;
			$result = $model->save($post);
			if ($result) {
				$this->success('编辑成功', U('showList', 3));
			} else {
				$this->error('编辑失败');
			};

		} else {
//b编辑后的操作
			$get = I('get.');
			$model = M('User');
			$modelDept = M('Dept');
			$mdatas = $model->select();
			$info = $model->find($get['id']);
//			$infoDept = $modelDept->find($get['dept_id']);
			$resultDept = $modelDept->select();
//		dump($resultDept) ;die;
			$this->assign('info', $info);
			$this->assign('resultDept', $resultDept);
			$this->display();
		}
	}

	public function delete()
	{
		$get = I('get.');
//		dump($get);die;
		$model = D('User');
		$result = $model->delete($get['id']);
//		dump($result);die;
		if ($result) {
			$this->success('删除成功', U('showList', 3));
		} else {
			$this->error('删除失败');
		}
	}
}